#!/usr/bin/env python
# -*- coding:utf-8 -*-

from .generator import py_gen

def parse_csv(csv_fn, define):
	print csv_fn

from xml.etree.cElementTree import ElementTree
def get_define(tree, define):
	return tree.find('define/'+define)

def gen_define(tree, attr):
	ignore = bool(int(attr.get('ignore', '0')))
	name = attr.get('name', '')
	label = attr.get('label', '')
	define = attr['define']
	assert define
	
	if ignore:
		return None

def parse_field_define(xml_fn):
	tree = ElementTree()
	tree.parse(xml_fn)
	fields = tree.find('fields')
	for field in fields.getiterator('field'):
		print field.attrib['label']
	print xml_fn

def process(csv_fn):
	xml_fn = csv_fn + u'2code.xml'
	define = parse_field_define(xml_fn)
	mod = parse_csv(csv_fn, define)
	output_fn = csv_fn.rsplit('.')[0] + '.py'
	with open(output_fn, 'w') as out:
		py_gen(mod, out)

